﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PanelView : MonoBehaviour
{

    public PanelItem panelItem;
    
    
    /// <summary>
    /// 设置面板
    /// </summary>
    /// <param name="areaPanelView"></param>
    public void SetAreaPanelView(AreaPanelView areaPanelView)
    {
        this.areaPanelView = areaPanelView;
    }

    /// <summary>
    /// 设置关系
    /// </summary>
    public void SetPanelViewRelation()
    {
        if (!panelItem.bindId.Equals("") && !panelItem.bindAreaPanelId.Equals("") )
        {
            this.basePanel= areaPanelView.TryToGetPanel(panelItem.bindId, panelItem.bindAreaPanelId);
        }
    }
    
    
    public void SetPanelItem(string id,PanelItem panelItem ,AreaPanelView areaPanelView)
    {
        this.panelItem = panelItem;
        this.areaPanelView = areaPanelView;
        Initialized(id);
    }
    
    
    
    private Text _text;
    private Image _image;

    public Text GetText
    {
        get
        {
            if(_text == null)
                _text = transform.GetChild(0).GetComponent<Text>();
            return _text;
        }
    }

    public Image GetIcon
    {
        get
        {
            if(_image == null)
                _image = transform.GetComponent<Image>();
            return _image;
        }
    }

    

    public void OnValidate()
    {
        UpdateTextView();
        UpdateImage();

        this.name = panelItem.id;
    }
    
    void UpdateTextView()
    {
        if (panelItem.cubeNum > 0)
        {
            GetText.text = panelItem.money +"/ <color=red>"+ panelItem.cubeNum+"</color> \n "+ panelItem.point;
        }
        else
        {
            GetText.text = panelItem.money.ToString();
        }
    }

   [Header("当前位置")] public Vector2 viewPos;


   /// <summary>
   /// 删除
   /// </summary>
   public void Delete()
   {
       areaPanelView.RemovePanelView(this);
       gameObject.SetActive(false);
       name = "[delete]";
   }

   /// <summary>
    /// 应用该点
    /// </summary>
    public void ApplyViewPos()
    {
        panelItem.point = viewPos;
    }
    
    
    public void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(transform.position,GetComponent<RectTransform>().sizeDelta);
        if(_drag)
            DragPanel();
        _drag = true;
    }
    
   [SerializeField] private AreaPanelView areaPanelView;
   [Header("基于")] public PanelView basePanel;
   
   private bool _drag;
   
   

   /// <summary>
   /// 更新数据
   /// </summary>
    void UpdateInfo()
    {
        if (basePanel == null)
        {
            panelItem.BindPanelItem(null);//绑定
        }
        else
        {
            panelItem.BindPanelItem(basePanel.panelItem);//绑定
        }
       
    }
    
    //更新布局
    public void UpdateLayOut()
    {
        areaPanelView.LayOutGrid();
        UpdateInfo();
    }

    public void Initialized(string id)
    {
        panelItem.Initialize(id, areaPanelView.GetAreaPanel.Id);
    }
    
    
    
    public void DragPanel()
    {
        if (areaPanelView == null)
            areaPanelView = transform.parent.GetComponent<AreaPanelView>();
        viewPos = areaPanelView.PickPanelItemPoint(this);
    }
    
    //获取颜色
    public Color GetColor()
    {
        switch (panelItem.panelType)
        {
            case PanelType.Road:
                if(panelItem.roadType==RoadType.None)
                    return Color.green;
                else
                    return new Color(1,.92f,.16f,.8f);
            case PanelType.Bridge:
                return Color.yellow;
            case PanelType.Other:
                return  Color.gray;
            default:
                return Color.clear;//没有颜色
        }
    }

    void UpdateImage()
    {
        if (panelItem.panelType != PanelType.Road)
        {
            panelItem.roadType = RoadType.None;
        }

        if (panelItem.panelType == PanelType.Water)
        {
            GetText.gameObject.SetActive(false);
        }
        else
        {
            GetText.gameObject.SetActive(true);
        }

        if (panelItem.lockType == LockType.Coin)
        {
            GetText.color = Color.black;
        }
        else
        {
            GetText.color = Color.white;
        }
        
        //_image.sprite = panerlManager.GetSpriteBy(roadType);
        GetIcon.sprite = SpriteManager.TryGetSpriteByName(panelItem.roadType);
        GetIcon.color = GetColor();
    }
    
    
}
